<template>
	<view>
		<view class="input">
			<u--input placeholder="请输入电话号码" type="number" @blur="leaveMobile" maxlength=12 border="surround" v-model="mobile" @change="changeMobile" clearable>
			</u--input>
		</view>
		<view class="wrap">
			<u-toast ref="uToast"></u-toast>
			<u-code :seconds="seconds" @end="end" @start="start" ref="uCode" @change="codeChange"></u-code>
			<u-button type="primary" :disabled="isDisable" @tap="getCode">{{tips}}</u-button>
		</view>
		<view class="certify">
			<u-code-input class="certigyInput" v-model="value3" @change="change" @finish="finish" :maxlength="6">
			</u-code-input>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tips: '',
				// refCode: null,
				seconds: 50,
				mobile: "",
				isDisable: true,
				value3: "",
				code: ""
			}
		},
		methods: {
			leaveMobile(){
				let that = this;
				if(this.mobile === "") return ;
				if(this.mobile.length < 7) {
					this.$Inter.JerryAlert("电话号码长度有误")
					this.mobile = "";
					return ;
				}
				this.$JerryRequest({
					url:"/checkPhoneByUser?mobile="+that.mobile
				}).then(res => {
					if(res.data.reqstatus == false){
						that.$Inter.JerryAlert("手机号没有注册过");
						that.mobile = "";
						that.isDisable = true;
						return ;
					}else {
						that.isDisable = false;
					}
				})
			},
			changeMobile(e) {
			},
			change(e) {},
			finish(e) {
				let that = this;
				// console.log('输入结束，当前值为：' + e);
				if (this.code == e) {
					this.$Inter.JerryAlert("验证成功");
					setTimeout(() => {
						uni.navigateTo({
							url: "./setPassword?mobile=" + that.mobile
						})
					}, 1000)
				} else {
					this.$Inter.JerryAlert("验证码错误")
				}
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				this.isDisable = true;
				let that = this;
				setTimeout(() => {
					that.isDisable = false;
				}, 52000)
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				// uni.$u.toast('倒计时结束');
			},
			start() {
				let that = this;
				uni.$u.toast('倒计时开始');
				this.$JerryRequest({
					url: "/sendEmail"
				}).then(res => {
					that.code = JSON.stringify(res.data);
				})
			}
		}
	}
</script>

<style lang="scss">
	.input {
		margin-top: 10px;
		margin-bottom: 20px;
	}
	.wrap {
		padding: 24rpx;
	}

	.certify {
		display: flex;
		margin: 20px;
		justify-content: center;
	}

	.certigyInput {}
</style>
